home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / muds / pennmush.000 / pennmush-1.50-p8-linux.tar / pennmush / mkindx.c < prev    next >
C/C++ Source or Header  |  1992-07-26  |  2KB  |  85 lines

  1. #include  <stdio.h>
  2. #include  "help.h"
  3.  
  4. char line[LINE_SIZE + 1];
  5. main(argc, argv)
  6.     int argc;
  7.     char *argv[];
  8. {
  9.   long pos;
  10.   int i, n, lineno, ntopics;
  11.   char *s, *topic;
  12.   help_indx entry;
  13.   FILE *rfp, *wfp;
  14.   char c;
  15.  
  16.   if(argc < 2 || argc > 3) {
  17.     printf("Usage:\tmkindx <file_to_be_indexed> <output_index_filename>\n");
  18.     exit(-1);
  19.   }
  20.  
  21.   if ((rfp = fopen(argv[1], "r")) == NULL) {
  22.     fprintf(stderr, "can't open %s for reading\n", argv[1]);
  23.     exit(-1);
  24.   }
  25.   if ((wfp = fopen(argv[2], "w")) == NULL) {
  26.     fprintf(stderr, "can't open %s for writing\n", argv[2]);
  27.     exit(-1);
  28.   }
  29.   pos = 0L;
  30.   lineno = 0;
  31.   ntopics = 0;
  32.  
  33.   /* try to prevent accidental clobbering if user reverses file order */
  34.   c = getc(rfp);
  35.   if (c != '&') {
  36.     printf("%s is probably not a text file.\n", argv[1]);
  37.     printf("Usage:\tmkindx <file_to_be_indexed> <output_index_filename>\n");
  38.     fclose(rfp);
  39.     fclose(wfp);
  40.     exit(-1);
  41.   }
  42.   ungetc(c, rfp);
  43.  
  44.   while (fgets(line, LINE_SIZE, rfp) != NULL) {
  45.     ++lineno;
  46.  
  47.     n = strlen(line);
  48.     if (line[n - 1] != '\n') {
  49.       fprintf(stderr, "line %d: line too long\n", lineno);
  50.     }
  51.     if (line[0] == '&') {
  52.       ++ntopics;
  53.  
  54.       if (ntopics > 1) {
  55.     entry.len = (int) (pos - entry.pos);
  56.     if (fwrite(&entry, sizeof(help_indx), 1, wfp) < 1) {
  57.       fprintf(stderr, "error writing %s\n", argv[2]);
  58.       exit(-1);
  59.     }
  60.       }
  61.       for (topic = &line[1];
  62.        (*topic == ' ' || *topic == '\t') && *topic != '\0'; topic++) ;
  63.       for (i = -1, s = topic; *s != '\n' && *s != '\0'; s++) {
  64.     if (i >= TOPIC_NAME_LEN - 1)
  65.       break;
  66.     if (*s != ' ' || entry.topic[i] != ' ')
  67.       entry.topic[++i] = *s;
  68.       }
  69.       entry.topic[++i] = '\0';
  70.       entry.pos = pos + (long) n;
  71.     }
  72.     pos += n;
  73.   }
  74.   entry.len = (int) (pos - entry.pos);
  75.   if (fwrite(&entry, sizeof(help_indx), 1, wfp) < 1) {
  76.     fprintf(stderr, "error writing %s\n", argv[2]);
  77.     exit(-1);
  78.   }
  79.   fclose(rfp);
  80.   fclose(wfp);
  81.  
  82.   printf("%d topics indexed\n", ntopics);
  83.   exit(0);
  84. }
  85.